using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Esercitazione_CG_1
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ModelManagerSplitScreen : Microsoft.Xna.Framework.GameComponent
    {
        public List<CustomModel> models = new List<CustomModel>();

        CameraSplitScreen camera;
        Viewport viewport1, viewport2;

        public ModelManagerSplitScreen(Game game, CameraSplitScreen camera, Viewport viewport1, Viewport viewport2)
            : base(game)
        {
            this.camera = camera;
            this.viewport1 = viewport1;
            this.viewport2 = viewport2;
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Aggiunge un elemento alla lista di modelli da elaborare.
        /// </summary>
        /// <param name="modelName">Il modello da caricare dal content</param>
        /// <param name="world">Trasformazione da applicare</param>
        public void add(String modelName, Matrix world)
        {
            var model = (Game.Content.Load<Model>(modelName));

            //var fx = Game.Content.Load<Effect>("Effects\\effettoACavoloDiCane");

            //foreach (var mesh in model.Meshes)
            //{
            //    foreach (var part in mesh.MeshParts)
            //    {
            //        part.Effect = fx;
            //    }
            //}

            CustomModel temp = new CustomModel(model, world);

            models.Add(temp);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public void Draw()
        {
            
            Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            Game.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            Game.GraphicsDevice.BlendState = BlendState.Opaque;

            Game.GraphicsDevice.Viewport = viewport1;
            drawScene(camera, 0, 1);
            Game.GraphicsDevice.Viewport = viewport2;
            drawScene(camera, 1, 0);
        }

        protected void drawScene(CameraSplitScreen camera, int numCamera, int otherCamera)
        {
            foreach (CustomModel model in models)
            {
                foreach (ModelMesh mesh in model.model.Meshes)
                {
                    foreach (BasicEffect bsx in mesh.Effects)
                    {
                        bsx.World = model.world;
                        bsx.View = camera.view[numCamera];
                        bsx.Projection = camera.projection;
                        bsx.EnableDefaultLighting();
                        //Vector4 temp = new Vector4(camera.pos[1].X, camera.pos[1].Y, camera.pos[1].Z, 1);
                        //bsx.Parameters["World"].SetValue(model.world);
                        //bsx.Parameters["View"].SetValue(camera.view[numCamera]);
                        //bsx.Parameters["Projection"].SetValue(camera.projection);
                        //bsx.Parameters["LightPosition"].SetValue(temp);
                        //bsx.Parameters["EyePosition"].SetValue(camera.pos[numCamera]);
                    }
                    mesh.Draw();
                }
            }
        }

    }
}
